/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nettest.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * 
 * @author ferren
 */
public  class Converter {
	private static Log log = LogFactory.getLog(Converter.class);
	
	private static String defaultFormatStr = "yyyy-MM-dd";
	private static String longDateFormatStr = "yyyy-MM-dd HH:mm:ss";
    //private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    //private static DateFormat longDateFormat = new SimpleDateFormat(
    //        "yyyy-MM-dd hh:mm:ss");

    public static String toString(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof String) {
            return (String) obj;
        } else {
            return obj.toString();
        }
    }

    public static String toNvlString(Object obj, String defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            return toString(obj);
        }
    }

    public static Boolean toBoolean(Object obj) {
        if (obj == null) {
            return null;
        } else if (obj instanceof Boolean) {
            return (Boolean) obj;
        } else {
            return null;
        }
    }

    public static Long toLong(Object obj) {
        if (obj == null) {
            return null;
        } else if (obj instanceof BigDecimal) {
            return new Long(((BigDecimal) obj).longValue());
        } else {
            return new Long(obj.toString());
        }
    }

    public static String toFmtLong(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof BigDecimal) {
            return new Long(((BigDecimal) obj).longValue()).toString();
        } else {
            return obj.toString();
        }
    }

    public static Double toDouble(Object obj) {
        if (obj == null) {
            return null;
        } else if (obj instanceof BigDecimal) {
            return new Double(((BigDecimal) obj).doubleValue());
        } else {
            return new Double(obj.toString());
        }
    }

    public static String toFmtDouble(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof BigDecimal) {
            return new Double(((BigDecimal) obj).doubleValue()).toString();
        } else {
            return obj.toString();
        }
    }

    public static Date toDate(Object obj) {
        DateFormat dateFormat = new SimpleDateFormat(defaultFormatStr);
        
        if (obj == null) {
            return null;
        } else if (obj instanceof Date) {
            return (Date) obj;
        } else if (obj instanceof String && "SYSDATE".equalsIgnoreCase(obj.toString().trim())) {
            return TimeUtil.getCurrentDate();	
        } else {
            try {
                return dateFormat.parse(obj.toString());
            } catch (ParseException e) {
            	log.error(e.toString());
                return null;
            }
        }
    }
    
    public static Date toLongDate(Object obj) {
        DateFormat longDateFormat = new SimpleDateFormat(longDateFormatStr);
        
        if (obj == null) {
            return null;
        } else if (obj instanceof Date) {
            return (Date) obj;
        } else if (obj instanceof String && "SYSDATE".equalsIgnoreCase(obj.toString().trim())) {
            return TimeUtil.getCurrentDate();	
        } else {
            try {
                return longDateFormat.parse(obj.toString());
            } catch (ParseException e) {
            	log.error(e.toString());
                return null;
            }
        }
    }

    public static String toFmtDate(Object obj) {
        DateFormat dateFormat = new SimpleDateFormat(defaultFormatStr);
        
        if (obj == null) {
            return "";
        } else if (obj instanceof java.util.Date) {
            return dateFormat.format((java.util.Date) obj);
        } else {
            return obj.toString();
        }
    }

    public static String toLongFmtDate(Object obj) {
        DateFormat longDateFormat = new SimpleDateFormat(longDateFormatStr);
        
        if (obj == null) {
            return "";
        } else if (obj instanceof java.util.Date) {
            return longDateFormat.format((java.util.Date) obj);
        } else {
            return obj.toString();
        }
    }
    
    public static Number truncateNumber(Number numValue, String precision) {
    	if (numValue == null ||precision == null) {
	    	return numValue;
	    }
	    
    	
    	String numStr = ConvertUtil.number2Str(numValue, "00000000.00");
    	int prec = Integer.parseInt(precision);
    	int idx = numStr.indexOf(".");
    	if (idx != -1) {
            numStr = numStr + "00000000000000000000";
            numStr = numStr.substring(0, idx+1+prec);
    	}
    	return Converter.toDouble(numStr);
    }	
    	

    
    public static Date truncateDate(Date dateValue, String precision) {
    	if (dateValue == null ||precision == null) {
	    	return dateValue;
	    }
		
    	Calendar c = Calendar.getInstance();
    	c.setTime(dateValue);
    	
		if ("Y".equalsIgnoreCase(precision)) {
		    c.set(Calendar.MONTH, 1);
		    c.set(Calendar.DAY_OF_MONTH, 1);
		    c.set(Calendar.HOUR_OF_DAY, 0);
		    c.set(Calendar.MINUTE, 0);
		    c.set(Calendar.SECOND, 0);
		    c.set(Calendar.MILLISECOND, 0);
		} else if ("M".equals(precision)) {
		    c.set(Calendar.DAY_OF_MONTH, 1);
		    c.set(Calendar.HOUR_OF_DAY, 0);
		    c.set(Calendar.MINUTE, 0);
		    c.set(Calendar.SECOND, 0);
		    c.set(Calendar.MILLISECOND, 0);
		} else if ("D".equals(precision)) {
		    c.set(Calendar.HOUR_OF_DAY, 0);
		    c.set(Calendar.MINUTE, 0);
		    c.set(Calendar.SECOND, 0);
		    c.set(Calendar.MILLISECOND, 0);
		} else if ("H".equalsIgnoreCase(precision)) {
		    c.set(Calendar.MINUTE, 0);
		    c.set(Calendar.SECOND, 0);
		    c.set(Calendar.MILLISECOND, 0);
		} else if ("m".equals(precision)) {
		    c.set(Calendar.SECOND, 0);
		    c.set(Calendar.MILLISECOND, 0);
		} 
		return c.getTime();
	}

    
    public static String toFmtNumber(Object number) {
    	if (number == null) {
            return "";
        } else if (number instanceof BigDecimal) {
        	String str = "";
        	DecimalFormat df1 = new DecimalFormat("##,###.00");
            str=df1.format(number);
        	return str;
        } else {
            return number.toString();
        }
    }
    
    public static String toFmtNumber(Object number,Object fmt) {
    	if (number == null) 
            return "";
    	String str = "";
    	DecimalFormat df1 = new DecimalFormat((String)fmt);
        str=df1.format(number);
    	return str;
    }
    
    public static Float toFloat(String number,String fmt) {
    	Float f = 0f;
    	if (number == null) 
            return f;
    	DecimalFormat df1 = new DecimalFormat((String)fmt);
        try {
			f = ((Number) df1.parseObject(number)).floatValue();
		} catch (ParseException e) {
		}
    	return f;
    }
    public static String toEmailDateTimeFmt(Object date) {
    	if (date == null) {
            return "";
        } else if (date instanceof java.util.Date) {
        	DateFormat longDateFormat = new SimpleDateFormat("yyyy年MM月dd日hh時mm分");
            return longDateFormat.format((java.util.Date) date);
        } else {
            return date.toString();
        }
    }
    public static String toEmailDateFmt(Object date) {
    	if (date == null) {
            return "";
        } else if (date instanceof java.util.Date) {
        	DateFormat longDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
            return longDateFormat.format((java.util.Date) date);
        } else {
            return date.toString();
        }
    }
    
    public static Date getSysdate() {
    	return new Date();
    }
    
    public static BigDecimal toScalePrecision(Double amount, int scale){
    	if(amount == null) return null;
    	if(scale<0){
    		throw new IllegalArgumentException("scale must larger than 0!");
    	}
    	BigDecimal bd = new BigDecimal(amount);
		return bd.setScale(scale, RoundingMode.HALF_UP);
    }
    
    public static BigDecimal toAmountPrecision(Double f){
    	if(f!=null)
    		return toScalePrecision(f,2);
    	else return null;
    }
    public static String toAmountPrecisionString(Double f){
    	if(f!=null)
    		return String.valueOf(toScalePrecision(f,2));
    	else return null;
    }
    public static void main(String[] args) {
		System.out.println(toFloat("90,999.915","##,###.00"));
		BigDecimal b = null;
		b = toScalePrecision(99999999.915, 2);
		System.out.println(b.floatValue());
//		b = toAmountPrecision(Double.valueOf((Float)99999999.91F));
//		System.out.println(b.toString());
//		System.out.println(Double.valueOf(b.toString()));
//		
//		System.out.println(Double.valueOf((Float)99999999.91F).);
	}
}
